﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Text;

namespace UnionPay4WP
{
    public class StringPlus
    {
        public static bool IsAllNumber(string s)
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] < '0' || s[i] > '9') return false;
            }
            return true;
        }
        public static bool IsAllHex(string s)
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (!((s[i] >= '0' && s[i] <= '9')||(s[i] >= 'a'&&s[i] <= 'f'))) return false;
            }
            return true;
        }

        //This Function does 2 things .First switch chinese bytes ,that is ,the bytes whose value is greater than 128 ,into
        //Chinese character of UTF-8 .Second switch '+' ,' ' ,'/' ,'?' ,'%' ,'#' ,'&' ,'=' to their url code.
        public static string pchar2chinese(char[]  pch)
        {
            StringBuilder stringbuilder = new StringBuilder();
            int i=0;
            while ('\0' != pch[i])
            {
                if ((pch[i]) > 127)
                {
                    //Combine 3 bytes to a UTF8 character
                    byte[] charArray = new byte[3];
                    charArray[0] = Convert.ToByte( pch[i++]);
                    charArray[1] = Convert.ToByte( pch[i++]);
                    charArray[2] = Convert.ToByte( pch[i++]);
                    string s = Encoding.UTF8.GetString(charArray,0,3); 
                    stringbuilder.Append(s);
                }
                else
                {
                    switch (pch[i])
                    {
                    //  case '+': stringbuilder.Append("%2b"); break;
                    //  case ' ': stringbuilder.Append("%20"); break;
                    //  case '/': stringbuilder.Append("%2f"); break;
                    //  case '?': stringbuilder.Append("%3f"); break;
                    //  case '%': stringbuilder.Append("%25"); break;
                        case '#': stringbuilder.Append("%23"); break;
                    //  case '&': stringbuilder.Append("%26"); break;
                    //  case '=': stringbuilder.Append("%3d"); break;
                        default:  stringbuilder.Append(pch[i]);break;
                    }
                    i++;
                }
            }

            return stringbuilder.ToString();
        }
    }
}
